package com.yehongyu.mytools.ui.app;

import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.swing.JButton;
import javax.swing.JPanel;

import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class SaveButton extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5733373040714983782L;
	JButton saveButton = new JButton("Save...");
	ListOne l1 = null;
	ListTwo l2 = null;
	org.jdom.input.SAXBuilder sb = new org.jdom.input.SAXBuilder();
	// 创建文档
	org.jdom.Document doc = null;
	public SaveButton(ListOne list1,ListTwo list2) {
		l1 = list1;
		l2 = list2;
		saveButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					doc = sb.build(new FileInputStream(
					"d:\\assettypedata.xml"));
//					 获得这个文档得跟元素
					org.jdom.Element e0 = doc.getRootElement();
					Element e1name = (Element)e0.getChildren().get(0);
					Element e1 = new Element(e1name.getName());
					System.out.println(e1name.getName());
					Element e2name1 = (Element)((Element)e0.getChildren().get(0)).getChildren().get(0);
					Element e2name2 = (Element)((Element)e0.getChildren().get(0)).getChildren().get(1);
					System.out.println(e2name1.getName());
					System.out.println(e2name2.getName());
//					System.exit(0);
					e0.addContent(e1);
					e1.addContent(new Element(e2name1.getName()).setText(l1.getValue()));
					e1.addContent(new Element(e2name2.getName()).setText(l2.getValue()));
					System.out.println(e0.getChildren().size());
					Format format = Format.getCompactFormat();
					format.setEncoding("UTF-8"); // 设置xml文件的字符为gb2312
					format.setIndent("        ");// 设置缩进

					XMLOutputter xml = new XMLOutputter(format);
					xml.output(doc, new FileOutputStream("d:\\assettypedata.xml"));
//				System.out.println(l1.getValue());
//				System.out.println(l2.getValue());
				} catch (Exception ex) {
					System.out.println(ex.getMessage());

				}
			}
		});
		add(saveButton);
		
	}

	public SaveButton(boolean isDoubleBuffered) {
		super(isDoubleBuffered);
		// TODO Auto-generated constructor stub
	}

	public SaveButton(LayoutManager layout) {
		super(layout);
		// TODO Auto-generated constructor stub
	}

	public SaveButton(LayoutManager layout, boolean isDoubleBuffered) {
		super(layout, isDoubleBuffered);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
